package coop.bancocredicoop.proyectos.gd.query.box;

import org.hibernate.Query;
import org.hibernate.classic.Session;
import org.hibernate.Transaction;

import coop.bancocredicoop.proyectos.gd.domain.Box;
import coop.bancocredicoop.proyectos.gd.hibernate.HibernateUtil;

public class FindBoxByCajaId {

	public Box run(String cajaId) throws Exception {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = null;
		Box result = null;

		try {
			tx = session.beginTransaction();

			result = run(cajaId, session);

			tx.commit();
		}
		catch (Exception e) {
			if (tx!=null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

		return result;

	}

	public Box run(String cajaId, Session session) throws Exception {

		Query query = session.createQuery("from Box where cajaId = :cajaId");
		query.setString("cajaId", cajaId);
		return (Box) query.uniqueResult();

	}

}
